package it.low.game.core.action;

import it.low.game.core.element.ActiveElement;
import it.low.game.core.location.MapPoint;

public class Patrol extends Move {
		
	private int step = 0;
	private MapPoint[] endPoints = null;

	public Patrol(MapPoint... endMapPositions){
		super(endMapPositions[0]);
		this.endPoints = endMapPositions;		
	}
	
	public boolean run(ActiveElement executor, long elapsedTime){
		boolean isFinish = false;
		double delta = executor.getProperty(ActiveElement.MOV_SPEED)*elapsedTime;
		if(endMapPosition!=null){
			if(executor.getMapPosition().isNear(endMapPosition,delta)){
				if(step<endPoints.length){					
					executor.setMapPosition(endMapPosition);
					this.setPath(endMapPosition,endPoints[step]);
					step++;					
				}else{
					executor.setMapPosition(endMapPosition);				
					this.setPath(endMapPosition,startMapPosition);					
					step=0;
				}
			}else{							
				double deltaX = delta*sinAlpha;
				double deltaY = delta*cosAlpha;							
				double newLocationX = executor.getMapPosition().getX() + deltaX;
				double newLocationY = executor.getMapPosition().getY() + deltaY;
				executor.setMapPosition(new MapPoint(newLocationX,newLocationY));
			}
		}		
		return isFinish;
	}		
}
